<template>
  <div class="upload-container" :class="size === 'small' ? 'size-small' : 'size-normal'">
    <el-upload
      :data="dataObj"
      :multiple="true"
      :limit="limit"
      :show-file-list="false"
      :before-upload="beforeUpload"
      :on-success="handleImageSuccess"
      class="image-uploader"
      drag
      action="https://upload.qiniup.com/"
      :accept="accept"
      :disabled="disabled"
      :class="disabled ? 'disabled' : ''"
    >
      <i class="el-icon-upload" />
      <div v-show="!imageUrl && size !== 'small'" class="el-upload__text">
        <span v-show="!placeholder">
          将文件拖到此处，或<em>点击上传</em>
        </span>
        <span v-show="placeholder">
          {{ placeholder }}
        </span>
      </div>
    </el-upload>
  </div>
</template>

<script>
import { getToken } from '@/api/qiniu'
import settings from '@/settings'

export default {
  name: 'MutiImageUpload',
  props: {
    value: {
      type: String,
      default: ''
    },
    showDelete: {
      type: Boolean,
      default: false
    },
    size: {
      type: String,
      default: 'normal'
    },
    placeholder: {
      type: String,
      default: ''
    },
    accept: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    },
    limit: {
      type: Number,
      default: 9
    },
    limitFunc: {
      type: Function,
      default: () => {
        return new Promise((resolve, reject) => {
          resolve()
        })
      }
    }
  },
  data() {
    return {
      tempUrl: '',
      dataObj: { token: '', key: '' },
      width: '',
      height: ''
    }
  },
  computed: {
    imageUrl() {
      return this.value
    }
  },
  methods: {
    rmImage() {
      this.emitInput('')
    },
    emitInput(val) {
      this.$emit('input', val)
      this.$emit('change', val)
    },
    handleImageSuccess(res) {
      this.tempUrl = `${settings.imgUrl}${res.key}`
      this.emitInput(this.tempUrl)
      const img = new Image()
      img.onload = () => {
        this.width = img.width
        this.height = img.height
        this.$emit('loaded', { width: this.width, height: this.height })
        this.$emit('ready', { url: this.tempUrl, width: this.width, height: this.height })
      }
      img.src = `${settings.imgUrl}${res.key}`
    },
    beforeUpload(file) {
      const _self = this
      return new Promise((resolve, reject) => {
        this.limitFunc(file).then((res) => {
          console.log(res)
          getToken().then(response => {
            // const key = response.data.fileKey
            const token = response.data.token
            _self._data.dataObj.token = token
            _self._data.dataObj.key = `${new Date().getTime()}${Math.floor(Math.random() * 999) + 1}${file.name}`
            resolve(true)
          }).catch(err => {
            console.log(err)
            reject(false)
          })
        }).catch((err) => { console.log(err) })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  @import "~@/styles/mixin.scss";
  .upload-container {
    width: 100%;
    position: relative;
    @include clearfix;
    &.size-small {
      width: 100px;
      height: 100px;

      .el-upload-dragger {
        height: 100px;

        .el-icon-upload {
          margin: 25px auto;
          font-size: 40px;
        }
      }

      .image-preview {
        height: 100px;
        width: 100px;
      }
    }
    .disabled {
      ::v-deep .el-upload-dragger {
        cursor: not-allowed;
      }
    }
    .image-uploader {
      width: 100%;
    }
    .image-preview {
      position: relative;
      height: 100%;
      width: 100%;
      // border: 1px dashed #d9d9d9;
      float: left;
      .image-preview-wrapper {
        position: relative;
        width: 100%;
        height: 100%;
        img {
          width: 100%;
          height: 100%;
          object-fit: contain;
        }
      }
      .image-preview-action {
        position: absolute;
        width: 100%;
        height: 100%;
        left: 0;
        top: 0;
        cursor: default;
        text-align: center;
        color: #fff;
        opacity: 0;
        font-size: 20px;
        background-color: rgba(0, 0, 0, .5);
        transition: opacity .3s;
        cursor: pointer;
        text-align: center;
        line-height: 200px;
        .el-icon-delete {
          font-size: 36px;
        }
      }
      &:hover {
        .image-preview-action {
          opacity: 1;
        }
      }
    }
  }
  .size-small {
    ::v-deep .el-upload-dragger {
      width: 100%;
      height: 100%!important;
    }
  }

</style>
